package io.renren.modules.hotel.wx;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.google.common.collect.Lists;
import com.google.common.io.Files;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import io.renren.common.utils.R;
import io.renren.modules.hotel.config.WxMaConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;

/**
 * @program: hotel-saas
 * @description:小程序临时素材接口
 * @author: taoz
 * @create: 2019-08-17 16:57
 **/
@Api(value = "小程序临时素材接口", tags = { "小程序临时素材接口" })
@RestController
@RequestMapping("/wx/media/{appid}")
public class WxMaMediaController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 上传临时素材
	 *
	 * @return 素材的media_id列表，实际上如果有的话，只会有一个
	 */
	@ApiOperation("上传临时素材")
	@PostMapping("/upload")
	public List<String> uploadMedia(@PathVariable String appid, HttpServletRequest request) throws WxErrorException {
		final WxMaService wxService = WxMaConfiguration.getMaService(appid);

		CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());

		if (!resolver.isMultipart(request)) {
			return Lists.newArrayList();
		}

		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
		Iterator<String> it = multiRequest.getFileNames();
		List<String> result = Lists.newArrayList();
		while (it.hasNext()) {
			try {
				MultipartFile file = multiRequest.getFile(it.next());
				File newFile = new File(Files.createTempDir(), file.getOriginalFilename());
				this.logger.info("filePath is ：" + newFile.toString());
				file.transferTo(newFile);
				WxMediaUploadResult uploadResult = wxService.getMediaService().uploadMedia(WxMaConstants.KefuMsgType.IMAGE, newFile);
				this.logger.info("media_id ： " + uploadResult.getMediaId());
				result.add(uploadResult.getMediaId());
			} catch (IOException e) {
				this.logger.error(e.getMessage(), e);
			}
		}

		return result;
	}

	/**
	 * 下载临时素材
	 */
	@ApiOperation("下载临时素材")
	@GetMapping("/download/{mediaId}")
	public File getMedia(@PathVariable String appid, @PathVariable String mediaId) throws WxErrorException {
		final WxMaService wxService = WxMaConfiguration.getMaService(appid);
		return wxService.getMediaService().getMedia(mediaId);
	}
}
